/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practtica4;

import BBDD.GestorBBDD;
import DTO.Acontecimiento;
import DTO.Colectivo;
import DTO.Documento;
import DTO.Personaje;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Usuario
 */
public class V1BusqSimpleEligeResult extends javax.swing.JFrame {

    /**
     * Creates new form V1BusqSimpleEligeResult
     */
    GestorBBDD gestor=new GestorBBDD();
    private ArrayList<Acontecimiento> resA;
    private ArrayList<Colectivo> resC;
    private ArrayList<Personaje> resP;
    private ArrayList<Documento> resD;
    String buscar;
    private Object[][] t;
    private javax.swing.JTable tablaMenu;
    
            public V1BusqSimpleEligeResult(String buscar,ArrayList<Acontecimiento> resA,ArrayList<Colectivo> resC,ArrayList<Personaje> resP,ArrayList<Documento> resD) {
        
    	 initComponents();
        tablaMenu = new JTable();
       
        this.buscar=buscar;
        this.resA=resA;
        this.resP=resP;
        this.resC=resC;
        this.resD=resD;        
        
        int filas=1;
        if(resA!=null) filas+= resA.size();
        if(resC!=null) filas+= resC.size();
        if(resD!=null) filas+= resD.size();
        if(resP!=null) filas+= resP.size();
        
        int columnas=4;
    	t=new Object[filas][columnas];
        String[]cabecera=cabecera();
   	tablaMenu.setModel(new DefaultTableModel(t,cabecera));
        this.cargarDatos();
       // tablaMenu.setVisible(true);
        tablaMenu.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
					int col=tablaMenu.columnAtPoint(e.getPoint());
					int fila=tablaMenu.rowAtPoint(e.getPoint());
					try {
						if(fila>=0 && col>=0){
                                                        String id2=(String)tablaMenu.getValueAt(fila, 0);
                                                        int id=Integer.parseInt(id2);
							llamaMuestra(id,(String)tablaMenu.getValueAt(fila, 2));	
						}
						else JOptionPane.showMessageDialog(null,"ERROR!!Seleccione una celda correcta");
						
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
			}

			private void llamaMuestra(int id,String tipo) throws ClassNotFoundException, SQLException {
				
				if(tipo.equals("Acontecimiento")){
                                    Acontecimiento a= gestor.recuperaAcontecimiento(id);
                                    V1BSimpleMuestraAcon va= new V1BSimpleMuestraAcon(a);
                                    va.setVisible(true);
                                    
                                } 
                                else if(tipo.equals("Colectivo")) {
                                    Colectivo c=gestor.recuperaColectivo(id);
                                     V1BSimpleMuestraColec vc= new V1BSimpleMuestraColec(c);
                                    vc.setVisible(true);    
                                } 
                                else if(tipo.equals("Personaje")) {
                                    Personaje p= gestor.recuperaPersonaje(id);
                                     V1BSimpleMuestraPers vp= new V1BSimpleMuestraPers(p);
                                     vp.setVisible(true);   
                                     
                                     
                                } 
                                else if(tipo.equals("Documento")) {
                                    Documento d=gestor.recuperaDocumento(id);
                                     V1BSimpleMuestraDoc vd= new V1BSimpleMuestraDoc(d);
                                    vd.setVisible(true);
                                } 
                                
                                
			}

		});
       	this.dispose();
    	jScrollPane2.setViewportView(tablaMenu);
        
    }
     
private String[] cabecera(){
    	String[] s=new String [] {"id", "Nombre", "Tipo"};
    	return s;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BotonVolver = new javax.swing.JButton();
        botonbusquedade = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        BotonVolver.setText("Volver");
        BotonVolver.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                BotonVolverMouseClicked(evt);
            }
        });

        jLabel1.setText("Busqueda de:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(161, 161, 161)
                .addComponent(BotonVolver)
                .addContainerGap(176, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(botonbusquedade, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(botonbusquedade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BotonVolver)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotonVolverMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_BotonVolverMouseClicked
       V1 v1=new V1();
       v1.setVisible(true);
       this.dispose();
    }//GEN-LAST:event_BotonVolverMouseClicked

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonVolver;
    private javax.swing.JTextField botonbusquedade;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    private void cargarDatos() {
        int i=0;
        this.botonbusquedade.setText(buscar);
        if(resA!=null)
            while(i<resA.size()){ 
                String id=resA.get(i).getId()+"";
                //t[i][1]=id;
                //t[i][2]=resA.get(i).getNombre().toString();
                //t[i][3]="Acontecieminto";
                tablaMenu.setValueAt(id, i, 0);
                tablaMenu.setValueAt(resA.get(i).getNombre().toString(), i, 1);
                String a="Acontecimeinto";
                tablaMenu.setValueAt(a, i, 2);
                i++;
            } 
        if(resC!=null)
            while(i<resC.size()){
                String id=resC.get(i).getId()+"";
                //t[i][1]=id;
                //t[i][2]=resC.get(i).getNombre().toString();
                //t[i][3]="Colectivo";
                tablaMenu.setValueAt(id, i, 0);
                tablaMenu.setValueAt(resC.get(i).getNombre().toString(), i, 1);
                String a="Colectivo";
                tablaMenu.setValueAt(a, i, 2);
                i++;
            }  
        if(resP!=null)
            while(i<resP.size()){
                String id=resP.get(i).getId()+"";
                //t[i][1]=id;
                //t[i][2]=resP.get(i).getNombre().toString();
                //t[i][3]="Personaje";
                tablaMenu.setValueAt(id, i, 0);
                tablaMenu.setValueAt(resP.get(i).getNombre().toString(), i, 1);
                String a="Personaje";
                tablaMenu.setValueAt(a, i, 2);
                i++;
            }   
        if(resD!=null)
            while(i<resD.size()){
                String id=resD.get(i).getId() +"";
                //t[i][1]=id;
                //t[i][2]="";
                //t[i][3]="Documento";
                tablaMenu.setValueAt(id, i, 0);
                tablaMenu.setValueAt("", i, 1);
                String a="Documento";
                tablaMenu.setValueAt(a, i, 2);
                i++;
            }        
        
        
    }
}
